/**
*    CTR - Centro Tecnologia Rocci
*    Open Source Java Classic Game Toolkit
*
*    (C) 2010, CTR - Centro Tecnologia Rocci
*
*    This library is free software; you can redistribute it and/or
*    modify it under the terms of the GNU Lesser General Public
*    License as published by the Free Software Foundation;
*    version 2.1 of the License.
*
*/
package it.ctr.game.toolkit.level.editor;

import it.ctr.game.toolkit.BrickBox;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.*;

import com.mojang.mario.level.Level;

public class BrickViewer extends JComponent implements MouseListener, MouseMotionListener
{
    private static final long serialVersionUID = -1234000000000000010L;

    private int x = -1;
    private int y = -1;
    
    public byte selectedBrick;
    private LevelEditor brickChangedListener;

    public BrickViewer()
    {
        Dimension size = new Dimension(Level.BLOCK_SIZE*11, Level.BLOCK_SIZE*8);
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    public void addNotify()
    {
        super.addNotify();
        BrickBox.init(getGraphicsConfiguration());
    }

    public void paintComponent(Graphics g)
    {        
        for (int x=0; x<11; x++)
            for (int y=0; y<8; y++)
            {
                g.drawImage(BrickBox.levelBrick[x][y], (x * Level.BLOCK_SIZE), (y * Level.BLOCK_SIZE), null);
            }

        g.setColor(Color.BLUE);
        int xPickedTile = (selectedBrick&0xff)%Level.BLOCK_SIZE;
        int yPickedTile = (selectedBrick&0xff)/Level.BLOCK_SIZE;
        g.drawRect(xPickedTile * Level.BLOCK_SIZE, yPickedTile * Level.BLOCK_SIZE, Level.BLOCK_SIZE-1, Level.BLOCK_SIZE-1);

        g.setColor(Color.BLACK);
        g.drawRect(x * Level.BLOCK_SIZE - 1, y * Level.BLOCK_SIZE - 1, Level.BLOCK_SIZE+1, Level.BLOCK_SIZE+1);
    }

    public void mouseClicked(MouseEvent e)
    {
    }

    public void mouseEntered(MouseEvent e)
    {
    }

    public void mouseExited(MouseEvent e)
    {
        x = -1;
        y = -1;
        repaint();
    }

    public void mousePressed(MouseEvent e)
    {
        x = e.getX() / Level.BLOCK_SIZE;
        y = e.getY() / Level.BLOCK_SIZE;
        
        setSelectedBrick((byte)(x+y*16));
        repaint();
    }

    public void mouseReleased(MouseEvent e)
    {
    }

    public void mouseDragged(MouseEvent e)
    {
        x = e.getX() / Level.BLOCK_SIZE;
        y = e.getY() / Level.BLOCK_SIZE;

        repaint();
    }

    public void mouseMoved(MouseEvent e)
    {
        x = e.getX() / Level.BLOCK_SIZE;
        y = e.getY() / Level.BLOCK_SIZE;
        repaint();
    }

    public void setSelectedBrick(byte block)
    {
        selectedBrick = block;
        repaint();
        if (brickChangedListener!=null)
            brickChangedListener.setSelectedBrick(selectedBrick);
    }

    public void addTilePickChangedListener(LevelEditor editor)
    {
        this.brickChangedListener = editor;
        if (brickChangedListener!=null)
            brickChangedListener.setSelectedBrick(selectedBrick);
    }
}